package com.wjm.api.ThreadPackage;

/**
 * Data:2022年07月25日
 * Author:wjm
 * Content: Thread相关的API
 *
 *
 *  Thread常用方法：
 *          public final String getName()      获取当前线程的名称
 *          static Thread currentThread()      获取当前正在执行的线程
 *          public void setName(String name)   设置线程名称
 *
 *
 *  设置线程名称的方式：
 *          1）setName(String name)方法
 *          2）创建带有参数的构造方法，传递线程的名称，原理是调用父类的有参构造，把线程的名称传给父类，让父类来修改名称
 *
 */
public class ThreadDemo1 extends Thread {
    public ThreadDemo1(){

    }

    public ThreadDemo1(String name){
        //将线程名称传给父类的有参构造
        super(name);
    }

    @Override
    public void run() {
        //获取当前正在执行线程的引用
        Thread t = Thread.currentThread();
        //获取当前线程的名称
        String name = t.getName();
        System.out.println("当前线程名称是："+name);
    }

    public static void main(String[] args) {
        //创建线程对象
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        ThreadDemo1 threadDemo2 = new ThreadDemo1();
        ThreadDemo1 threadDemo3 = new ThreadDemo1();
        //将线程名称传给父类的有参构造
        ThreadDemo1 threadDemo4 = new ThreadDemo1("cwt");

        //设置线程名称
        threadDemo3.setName("wjm");

        //启动线程
        threadDemo1.start();
        threadDemo2.start();
        threadDemo3.start();
        threadDemo4.start();
    }
}
